﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// Flow 的摘要说明。
    /// </summary>
    public partial class EditFlowPage : Page
    {
        private int _flowId;

        protected override void OnInit(EventArgs e)
        {
            cmdNext.Click += cmdNext_Click;
            cmdFinish.Click += cmdFinish_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            _flowId = QSUtil.GetPositiveInt32("FlowID");
            if (!Page.IsPostBack)
            {
                Bangding();
            }

            base.OnLoad(e);
        }

        private void Bangding()
        {
            int StyleID = 0;
            if (_flowId > 0)
            {
                Flow f = Flow.GetFlow(_flowId);
                if (f != null)
                {
                    txtFlowName.Text = f.Name;
                    txtFlowRemark.Text = f.Description;
                    StyleID = f.StyleID;
                }
            }
            FillStyle(StyleID);
        }

        private void FillStyle(int styleId)
        {
            IList<FlowStyle> list = FlowStyle.GetAllFlowStyles();
            dpStyle.Items.Clear();
            foreach (var style in list)
            {
                dpStyle.Items.Add(new ListItem(style.Name, style.ID.ToString()));
                if (style.ID == styleId)
                {
                    dpStyle.SelectedIndex = dpStyle.Items.Count - 1;
                }
            }
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                var userName = SiteContext.Current.UserName;

                if (_flowId <= 0)
                {
                    _flowId = Flow.AddFlow(txtFlowName.Text, txtFlowRemark.Text, userName, Int32.Parse(dpStyle.Items[dpStyle.SelectedIndex].Value));
                    if (_flowId > 0)
                    {
                        Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=1");
                    }
                }
                else
                {
                    if (Flow.UpdateFlow(_flowId, txtFlowName.Text, txtFlowRemark.Text, userName, Int32.Parse(dpStyle.Items[dpStyle.SelectedIndex].Value)) == 0)
                    {
                        Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=1");
                    }
                }

                Response.Redirect("Listview.aspx");
            }
        }

        private void cmdFinish_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                String UserName = SiteContext.Current.UserName;

                if (_flowId <= 0)
                {
                    _flowId = Flow.AddFlow(txtFlowName.Text, txtFlowRemark.Text, UserName, Int32.Parse(dpStyle.Items[dpStyle.SelectedIndex].Value));
                    if (_flowId > 0)
                    {
                        Response.Redirect("ManageFlow.aspx?FlowID=" + _flowId);
                    }
                }
                else
                {
                    if (Flow.UpdateFlow(_flowId, txtFlowName.Text, txtFlowRemark.Text, UserName, Int32.Parse(dpStyle.Items[dpStyle.SelectedIndex].Value)) == 0)
                    {
                        Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=1");
                    }
                    Response.Redirect("ManageFlow.aspx?FlowID=" + _flowId);
                }
            }
        }
    }
}